using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using CommonBehaviors.Actions;
using Styx;
using Styx.Combat.CombatRoutine;
using Styx.Logic;
using Styx.Logic.Combat;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
using TreeSharp;

namespace FelMaster.Classes.Rogue
{
    class Combat : TalentBase
    {

        public override string Name
        {
            get { return "Combat V3.0"; }
        }

        public override string KeySpell
        {
            get { return "Blade Flurry"; }
        }
        
        public override string Help
        {
            get
            {
                return 
                    "   Focus a target to use Tricks of the Trade\n" +
                    "   1: Standard Combat Rotation\n" +
                    "   2: Use of Killing Spree\n" +
                    "-----------------------------------------";
            }
        }
        
        public bool Debuff(String debuffname)
        {
            return Me.CurrentTarget.Auras.ContainsKey(debuffname);
        }
        
        private static bool WillEnergyCap
        {
            get { return Lua.GetReturnVal<float>("return GetPowerRegen()", 1) > 25f; }
        }
        
        public bool BleedCheck()
        {
            return (Debuff("Mangle") || Debuff("Trauma") || Debuff("Gore") || Debuff("Stampede") || Debuff("Tendon Rip") || Debuff("Hemorrhage"));
        }

        public bool ArmorCheck()
        {
            return (Debuff("Corrosive Spit") || Debuff("Tear Armor") || Debuff("Expose Armor") || Debuff("Faerie Fire") || Debuff("Sunder Armor"));
        }

        public override Composite SingleRotation
        {
            get {
                return new Sequence(
                    SC.BurstSequence(
                        SC.CastSpell("Killing Spree", a => Me.CurrentEnergy < 35 && !SC.PlayerHasBuff("Adrenaline Rush") && SC.PlayerBuffTimeLeft("Slice and Dice") > 4, "Killing Spree"),
                        SC.CastSpell("Adrenaline Rush", a => !(SC.PlayerHasBuff("Time Warp") || SC.PlayerHasBuff("Bloodlust") || SC.PlayerHasBuff("Heroism") || SC.PlayerHasBuff("Ancient Hysteria")) && Me.CurrentEnergy < 35, "Adrenaline Rush")
                    ),
                    
                    new PrioritySelector(
                        new Decorator(x => SC.PlayerIsChanneling, new TreeSharp.Action(a => SC.Log("Channeling"))),
                        SC.UseEngineerGloves(a => true, "Engineer's glove enchant"),
                        SC.CastSpell("Blood Fury", a => true, "Blood Fury"),
                        SC.CastSpell("Lifeblood", a => true, "Lifeblood"),
                        SC.CastSpell("Berserking", a => true, "Berserking"),
                        SC.CastSpell("Slice and Dice", a => !SC.PlayerHasBuff("Slice and Dice"), "Slice and Dice"),
                        SC.CastSpell("Slice and Dice", a => !SC.TargetHasDebuff("Revealing Strike") && SC.PlayerBuffTimeLeft("Slice and Dice") < 3, "Slice and Dice"),
                        SC.CastSpell("Redirect", a => SC.CanCast("Slice and Dice") && Me.ComboPoints == 0, "Redirect"),
                        SC.CastSpell("Arcane Torrent", a => Me.CurrentEnergy < 25 && !SC.PlayerHasBuff("Adrenaline Rush"), "Arcane Torrent"),
                        SC.RunMacroText("/cast [@focus] Tricks of the Trade", a => SC.CanCast("Tricks of the Trade") && StyxWoW.Me.FocusedUnitGuid != 0 && !StyxWoW.Me.FocusedUnit.Dead && StyxWoW.Me.FocusedUnit.Attackable && StyxWoW.Me.FocusedUnit.InLineOfSight && !StyxWoW.Me.FocusedUnit.Mounted, "Tricks of the Trade"),
                        SC.CastAreaSpell("Fan of Knives", 8, false, 8, 0.0, 0.0, ret => !SC.PlayerHasBuff("Blade Flurry"), "Fan of Knives"),
                        SC.CastAreaSpell("Fan of Knives", 8, false, 11, 0.0, 0.0, ret => true, "Fan of Knives"),
                        SC.CastSpell("Fan of Knives", a => SC.PlayerHasBuff("Killing Spree") && Me.CurrentEnergy > 85, "Fan of Knives"),
                        SC.CastSpell("Eviscerate", a => Me.ComboPoints == 5 && SC.PlayerHasBuff("Deep Insight"), "Eviscerate"),
                        SC.CastSpell("Rupture", a => (Me.CurrentTarget.CurrentHealth > 1000000 || Me.CurrentTarget.Name.Contains("Dummy")) && Me.ComboPoints == 5 && !SC.TargetHasDebuff("Rupture") && !SC.PlayerHasBuff("Blade Flurry") && !SC.PlayerHasBuff("Deep Insight") && ((BleedCheck()) || !WillEnergyCap), "Rupture"),
                        SC.CastSpell("Eviscerate", a => Me.ComboPoints == 5, "Eviscerate"),
                        SC.CastSpell("Revealing Strike", a => Me.ComboPoints == 4 && !SC.TargetHasDebuff("Revealing Strike"), "Revealing Strike"),
                        SC.CastSpell("Sinister Strike", a => Me.ComboPoints < 4, "Sinister Strike"),
                        SC.CastSpell("Sinister Strike", a => Me.ComboPoints == 4 && SC.TargetHasDebuff("Revealing Strike"), "Sinister Strike"),
                        SC.CastSpell("Feint", a => Me.CurrentEnergy < 25 && !SC.PlayerHasBuff("Adrenaline Rush"), "Feint")
                    )
                );
            }
        }

        public override Composite Buffs
        {
            get
            {
                return new PrioritySelector(
                    //XXX put your buff rotation here!
                    
                );
            }
        }

        public override Composite PVPRotation
        {
            get { throw new NotImplementedException(); }
        }

        public override Composite PVERotation
        {
            get
            {
                return SingleRotation;
            }
        }
    }
}